var express = require('express');
var router = express.Router();
var {myPetModel,varietyTypeModel,caseModel,pets_hosp,userModel} = require('../../models/db')
var multiparty = require('multiparty')
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
router.get('/myPetList',async(req,res)=>{
  let {uid}=req.query
  let data=[]
  if(uid){
    data = await myPetModel.find({uid:uid}).populate('eid').populate('vid').populate('uid')
  }else{
    data = await myPetModel.find().populate('eid').populate('vid').populate('uid')
  }

  res.send({
    code:200,
    data
  })
})
router.post('/addMyPet',(req,res)=>{
  myPetModel.create(req.body)
  res.send({
    code:200,
    msg:'添加成功'
  })
})

router.put('/updateMyPet',async(req,res)=>{
  let _id = req.query.id
  await myPetModel.updateOne({_id},req.body)
  res.send({
    code:200,
    msg:'修改成功'
  })
})
router.get('/variety',async(req,res)=>{
  let typeData = await varietyTypeModel.find()
  res.send({
    code:200,
    typeData
  })
})
router.get('/caseList',async(req,res)=>{
  let {mid}=req.query
  let data=[]
  if(mid){
    data = await caseModel.find({mid:mid}).populate('mid').populate('hospital')
  }else{
    data = await caseModel.find().populate('mid').populate('hospital')
  }
  res.send({
    code:200,
    data
  })
})


router.post('/addCase',(req,res)=>{
  caseModel.create(req.body)
  res.send({
    code:200,
    msg:'添加成功'
  })
})
router.delete('/delCase',async(req,res)=>{
  let _id=req.query.id
  await caseModel.deleteOne({_id})
  res.send({
    code:200,
    msg:'删除成功'
  })
})

router.get('/petsList',async(req,res)=>{
  let data = await pets_hosp.find()
  res.send({
    code:200,
    data
  })
})
router.get('/user',async(req,res)=>{
  let {id}=req.query
  let data=[]
  if(id){
    data = await userModel.find({_id:id})
  }else{
    data = await userModel.find()
  }
  res.send({
    code:200,
    data
  })
})
module.exports = router;
